package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.domain.*;
import com.example.dto.UserCount;
import com.example.service.CommodityService;
import com.example.service.UserService;
import com.example.utils.MapSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("toUser")
    public String toSeek() {
        return "user/user";
    }

    @GetMapping("getAllUser")
    @ResponseBody
    public TableData getAll() {
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setData(userService.list(null));
        return tableData;
    }

    @GetMapping("/getPage")
    @ResponseBody
    public TableData getPage(Integer page, Integer limit, String username) {

        return userService.getPage(page, limit, username);
    }

    @PostMapping("deleteUser")
    @ResponseBody
    public DataJson deleteCategory(@RequestParam("id") Integer id) {
        userService.removeById(id);
        DataJson dataJson = new DataJson();
        dataJson.setCode(0);
        return dataJson;
    }

    @PostMapping("batchDeleteUser")
    @ResponseBody
    public DataJson batchDeleteCategory(@RequestParam("ids[]") Integer ids[]) {
//        System.out.println("testd------>"+ Arrays.toString(ids));

        userService.removeByIds(Arrays.asList(ids));
        DataJson dataJson = new DataJson();
        dataJson.setCode(0);
        dataJson.setMsg("删除成功");
        return dataJson;
    }

    @PostMapping("updateUser")
    @ResponseBody
    public DataJson updateCommodity(@RequestBody User user) {
        DataJson dataJson = new DataJson();
        userService.updateById(user);
        dataJson.setCode(0);
        dataJson.setMsg("修改成功");

        return dataJson;

    }

    @PostMapping("addUser")
    @ResponseBody
    public DataJson addCategory(@RequestBody User user) {
        userService.save(user);
        DataJson dataJson = new DataJson();
        dataJson.setCode(0);
        return dataJson;
    }

    @PostMapping("register")
    @ResponseBody
    public Result register(String username, String password) {
        return userService.register(username, password);
    }

    @PostMapping("edit")
    @ResponseBody
    public Result edit(Integer userId, String birthday, String phone, Integer genderId) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(userId != null, User::getId, userId);

        List<User> list = userService.list(lambdaQueryWrapper);

        User user = list.get(0);

        user.setBirthday(birthday);
        user.setPhone(phone);

        if (genderId == 0) {
            user.setGender(null);
        } else if (genderId == 1) {

            user.setGender(1);

        } else if (genderId == 2) {

            user.setGender(0);

        }

        userService.updateById(user);

        return Result.success("修改成功");

    }

    @PostMapping("editPassword")
    @ResponseBody
    public Result editPassword(Integer userId, String password1, String password2) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(userId != null, User::getId, userId);

        User user = userService.getById(userId);

        if (user.getPassword().equals(password1)) {
            user.setPassword(password2);
            userService.updateById(user);
            return Result.success("修改密码成功");
        } else {
            return Result.error("输入的原密码错误");
        }

    }

    @Autowired
    private CommodityService commodityService;

    @GetMapping("publishCount")
    @ResponseBody
    public Result publishCount() {
        List<Commodity> list = commodityService.list();
        HashMap<Integer, Integer> map = new HashMap<>();
        List<Integer> num = new ArrayList();
        for (Commodity commodity : list) {
            num.add(commodity.getUserId());
        }


        for (Integer i : num) {
            map.put(i, map.getOrDefault(i, 0) + 1);
        }

        Map<Integer, Integer> sortMap = MapSort.sortMap(map);

        List<Integer> idList = new ArrayList<>(sortMap.keySet());

        List<UserCount> userCountList = new ArrayList<>();


        for (Integer id : idList) {

            User user = userService.getById(id);
            UserCount userCount = new UserCount();

            userCount.setId(user.getId());
            userCount.setUsername(user.getUsername());
            userCount.setGender(user.getGender());

            userCount.setPublishCount(map.get(id));

            userCountList.add(userCount);
        }


        return Result.success(userCountList);
    }

    @GetMapping("buyCount")
    @ResponseBody
    public Result buyCount() {

        List<Commodity> list = commodityService.list();
        HashMap<Integer, Integer> map = new HashMap<>();
        List<Integer> num = new ArrayList();
        for (Commodity commodity : list) {
            if (commodity.getPurchaserId() != null) {
                num.add(commodity.getPurchaserId());
            }

        }


        for (Integer i : num) {
            map.put(i, map.getOrDefault(i, 0) + 1);
        }

        Map<Integer, Integer> sortMap = MapSort.sortMap(map);

        List<Integer> idList = new ArrayList<>(sortMap.keySet());

        List<UserCount> userCountList = new ArrayList<>();


        for (Integer id : idList) {

            User user = userService.getById(id);
            UserCount userCount = new UserCount();

            userCount.setId(user.getId());
            userCount.setUsername(user.getUsername());
            userCount.setGender(user.getGender());

            userCount.setBuyCount(map.get(id));

            userCountList.add(userCount);
        }
        return Result.success(userCountList);
    }

}
